#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

typedef struct proess
{
    int youXianJi;
    int shiJianPian;
    char neiRong[20];
}proess;

typedef struct list
{
    proess* a;
    int size;
    int capacity;
}list;

void sort_youXian(proess* a,int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[j].youXianJi>a[j+1].youXianJi)
            {
                proess t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    printf("YouXianJiPaiXuWanCheng\n");
}

list Init()
{
   list node;
   node.size=0;
   node.capacity=10;
   node.a= (proess*)malloc(sizeof(proess)*node.capacity);
   return node;
}

void check(list* ls)
{
    if(ls->size==ls->capacity)
    {
        ls->capacity*=2;
        proess* newNode=(proess*)realloc(ls->a,ls->capacity);
        if(newNode==NULL)
        {
            printf("扩容失败\n");
            return;
        }
        else
        {
            ls->a=newNode;
        }
    }
}

void Add(list* ls,proess data)
{
    check(ls);
    ls->a[ls->size++]=data;
    sort_youXian(ls->a,ls->size);
}

void Del(list* ls1,list* ls2)
{
    if(ls1->size==0)
        return ;
    Add(ls2,ls1->a[ls1->size]);
    ls1->size--;
}

void timeDelAll(list* ls)
{
    int i,n=ls->size;
    for(i=n-1;i>=0;i--)
    {
        ls->a[i].shiJianPian--;
    }
}

void checkTime(list* ls1,list* ls2)
{
    int n=ls1->size,i,j;
    for(i=n-1;i>=0;i--)
    {
        if(ls1->a[i].shiJianPian==0)
        {
            proess data=ls1->a[i];
            for(j=i;j<n;j++)
            {
                ls1->a[j]=ls1->a[j+1];
            }
            printf("DengDaiWanBiHuiDaoDuiLie\n");
            Add(ls2,data);
            checkTime(ls1,ls2);
            return ;
        }
    }

    timeDelAll(ls1);

}

int main()
{
    list ls1=Init();
    list ls2=Init();
    int n=10,i;
    proess data;
    for(i=0;i<n;i++)
    {
        data.shiJianPian=5;
        data.youXianJi=i;
        strcpy(data.neiRong,"proe");
        data.neiRong[4]=i+'0';
        //printf("%s\n",data.neiRong);
        Add(&ls1,data);
    }

    int time=100;
    while(time--)
    {
        int len=ls1.size;
        ls1.a[len-1].shiJianPian--;
        if(ls1.a[len-1].shiJianPian==0)
        {
            printf("%sShiJianJieShu\n",ls1.a[len-1].neiRong);
            Del(&ls1,&ls2);
        }
        else
        {
            printf("YunXing%s\n",ls1.a[len-1].neiRong);
        }

        checkTime(&ls2,&ls1);

    }



    return 0;
}
